jQuery(document).ready(function () {
    if (top != self) {
        top.location = self.location;
    }

    $('.page-container').on('click', 'button[class="submit_button"]', function () {
        submit();
    })

    function submit() {
        var username = $('.page-container form').find('.username').val();
        var password = $('.page-container form').find('.password').val();
        if (username == '') {
            $('.page-container form').find('.error').fadeOut('fast', function () {
                $('.page-container form').find('.error').css('top', '27px');
            });
            $('.page-container form').find('.error').fadeIn('fast', function () {
                $('.page-container form').parent().find('.username').focus();
            });
            return;
        }

        if (password == '') {
            $('.page-container form').find('.error').fadeOut('fast', function () {
                $('.page-container form').find('.error').css('top', '96px');
            });
            $('.page-container form').find('.error').fadeIn('fast', function () {
                $('.page-container form').parent().find('.password').focus();
            });
            return;
        }
        let formData = {};
        $('.page-container form').serializeArray().map(item => {
            const ret = formData[item['name']] = item['value'];
            console.log(`${item['name']} : ${item['value']}`);
            return formData[item['name']] = item['value'];
        })
        formData.userPhone = '123456';
        console.log('请求 system/login 的参数：',formData);
        $.ajax({
            url: 'system/login',
            type: 'POST',
            dataType: 'json',
            data: formData,
            success: function (data) {
                if (data.success) {
                    // window.location.reload();
                    // 保存登录信息 2022年8月25日 15:41:31
                    // 登录数据模型如下：
                    // {
                    //     "loginName": "root",
                    //     "userPhone": "123456",
                    //     "loginPassword": "xdf123"
                    // }
                    localStorage.setItem("loginInfo",JSON.stringify(formData));
                    window.location.href = getHomeUrl();// 跳转到主页
                } else {
                    $('.page-container form').find('.message').text(data.message);
                    $('.page-container form').find('.message').fadeIn('fast', function () {
                        $('.page-container form').parent().find('.username').focus();
                    });
                }
            },
            error: function (err) {
                $('.page-container form').find('.message').text('请求服务器出现异常！');
                $('.page-container form').find('.message').fadeIn('fast', function () {
                    $('.page-container form').parent().find('.username').focus();
                });
            },
            complete: function () {
            }
        })
    }

    // $('.page-container form .username, .page-container form .userPhone, .page-container form .password').keyup(function (e) {
    $('.page-container form .username, .page-container form .password').keyup(function (e) {
        $(this).parent().find('.error').fadeOut('fast');
        $(this).parent().find('.message').fadeOut('fast');
        if (e.keyCode == 13) {
            let length = $('.page-container form input').length;
            let index = $('.page-container form input').index(this);
            if (index >= length - 1) {
                submit();
            } else {
                $('.page-container form input').eq(index + 1).focus();
            }
        }
    });
    $('.page-container form').parent().find('.username').focus();


});


function test(){
    console.log('当前域名的两种方法：');
    console.log(document.domain);
    console.log(window.location.host);

    console.log('当前URL的4种方式：');
    console.log(window.location.href);
    console.log(self.location.href);
    console.log(document.URL);
    console.log(document.location);
}

function skip(){
    const url = window.location.href.replace('login','home');
    console.log(url);
}

function getHomeUrl(){
    return window.location.href.replace('login','home');
}